package org.jprogger.myworld.api.response.handler;

import java.util.ArrayList;
import java.util.List;

import org.jprogger.myworld.api.ApiServiceInvocationException;
import org.jprogger.myworld.api.app.ApiParams;
import org.jprogger.myworld.api.model.City;
import org.jprogger.myworld.api.model.Country;
import org.jprogger.myworld.api.model.Location;
import org.jprogger.myworld.api.model.Region;
import org.jprogger.myworld.api.model.User;
import org.jprogger.myworld.api.request.ApiRequest;
import org.jprogger.myworld.api.response.ApiResponse;
import org.jprogger.myworld.api.response.AbstractApiResponseHandler;
import org.jprogger.myworld.api.response.ApiResponseData;
import org.jprogger.myworld.api.response.ApiResponseError;
import org.jprogger.myworld.api.response.DefaultApiResponse;
import org.jprogger.myworld.api.response.DefaultApiResponseData;
import org.jprogger.myworld.api.response.ParseResponseException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GetListUserInfoApiResponseHandler extends AbstractApiResponseHandler<List<User>> {

	@Override
	public ApiResponse<List<User>> handleResponse(ApiRequest request, String response) throws ApiServiceInvocationException {
		ApiResponseError apiError = null;
		ApiResponseData<List<User>> apiData = null;
		ApiResponse<List<User>> apiResponse = new DefaultApiResponse<List<User>>();
		String format = request.getRequestFormat();
		try 
		{
			apiError = validateResponse(response, format);
			if (apiError == null)
			{
				apiData = new DefaultApiResponseData<List<User>>();
				List<User> users = new ArrayList<User>();
				
				if (format.equals(ApiParams.JSON_REQUEST_FORMAT)) 
				{
					JSONArray jsonFriends = new JSONArray(response);
					for (int i = 0; i < jsonFriends.length(); i++) 
					{
						User user = new User();
						JSONObject jsonFriend = jsonFriends.getJSONObject(i);
						user.setUseId(jsonFriend.getString("uid"));
						user.setFirstName(jsonFriend.getString("first_name"));
						user.setLastName(jsonFriend.getString("last_name"));
						user.setNickName(jsonFriend.getString("nick"));
						user.setGender(jsonFriend.getString("sex"));
						user.setBirthday(jsonFriend.getString("birthday"));
						user.setPictureUrl(jsonFriend.getString("pic"));
						user.setPictureLargeUrl(jsonFriend.getString("pic_big"));
						user.setPictureSmallUrl(jsonFriend.getString("pic_small"));
						user.setUserHomepageUrl(jsonFriend.getString("link"));
						user.setRefererType(jsonFriend.getString("referer_type"));
						user.setRefererId(jsonFriend.getString("referer_id"));
						
						JSONObject jsonLocation = jsonFriend.getJSONObject("location");
						if (jsonLocation != null)
						{
							City city = new City();
							Region region = new Region();
							Country country = new Country();
							Location location = new Location();
							
							JSONObject jsonCountry = jsonLocation.getJSONObject("country");
							JSONObject jsonRegion  = jsonLocation.getJSONObject("region");
							JSONObject jsonCity    = jsonLocation.getJSONObject("city");
							
							if (jsonCity != null)
							{
								city.setId(jsonCity.getInt("id"));
								city.setName(jsonCity.getString("name"));
							}
							if (jsonRegion != null)
							{
								region.setId(jsonRegion.getInt("id"));
								region.setName(jsonRegion.getString("name"));
							}
							
							if (jsonCountry != null)
							{
								country.setId(jsonCountry.getInt("id"));
								country.setName(jsonCountry.getString("name"));
							}
							location.setCity(city);
							location.setRegion(region);
							location.setCountry(country);
							user.setLocation(location);
						}
						users.add(user);
						apiData.setData(users);
					}
				}
				else if (format.equals(ApiParams.XML_REQUEST_FORMAT))
				{
					// TODO: implement XML response format 
					throw new ApiServiceInvocationException("Unsupported response format");
				}
			}
			apiResponse.setError(apiError);
			apiResponse.setResponseData(apiData);
		} 
		catch (JSONException e)
		{
			throw new ApiServiceInvocationException("Bad response format", e);
		}
		catch (ParseResponseException e) 
		{
			throw new ApiServiceInvocationException("Unable to parse response", e);
		}
		return apiResponse;
	}
}
